<!DOCTYPE html>
<!--
  Copyright 2011 Google Inc. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <meta charset="UTF-8">

    <title>Fusion Tables API Example: Google Chart Tools Data Table</title>

    <link href="/apis/fusiontables/docs/samples/style/default.css"
        rel="stylesheet" type="text/css">
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', { packages: ['table'] });

	  	 	  
	  /**
 * Auto Complete using Fusion Table
 * Only returns unique names
 * @param {string} FTtableID The Id of the Fusion Table
 * @param {string} FTcolumnID The Fusion table column which will be searched for Auto Complete
 * @param {string} ACtagID The html ID name for the Auto Complete
 */
function AutoCompleteFT(FTtableID, FTcolumnID, ACtagID) { 
	var queryText = encodeURIComponent(
		"SELECT '" + FTcolumnID + "', COUNT() " +
		"FROM " + FTtableID + " GROUP BY '" + FTcolumnID + "'");
	alert(queryText);
	var query = new google.visualization.Query(
		'http://www.google.com/fusiontables/gvizdata?tq='  + queryText); 
	query.send(function getData(response) { 
	
	var table = new google.visualization.Table(
              document.getElementById('visualization'));
          table.draw(response.getDataTable(), {
            //showRowNumber: true,
			"page": "enable",
			"pageSize": 10
          });
		  
		alert(ACtagID);
		numRows = response.getDataTable().getNumberOfRows(); 
		// Create the list of results for display of autocomplete 
		results = []; 
		
		for(i = 0; i < numRows; i++) { 
			results.push(response.getDataTable().getValue(i, 0)) ; 
			alert(response.getDataTable().getValue(i, 0));
		}	 
		// Use the results to create the autocomplete options
		$("#"+ACtagID).autocomplete({ 
			source: results, 
			minLength: 2
		}); 
	});
 }

	  
      

	 function drawtable {
   var query = "select 'Utility', 'Supplier', 'Cost', 'Contract Length', 'State', 'Date' from 1rPtLXKVn-fJ11xVbNoNrmlDgT8-JjhJ_0e5LkEY order by 'Date' DESC";
        var team = document.getElementById('acomplete').value;
        if (team) {
          query += " WHERE 'Scoring Team' = '" + team + "'";
        }
        var queryText = encodeURIComponent(query);
        var gvizQuery = new google.visualization.Query(
            'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);

        gvizQuery.send(function(response) {
          var table = new google.visualization.Table(
              document.getElementById('visualization'));
          table.draw(response.getDataTable(), {
            //showRowNumber: true,
			"page": "enable",
			"pageSize": 10
          });
        });
      }
	  
	  function initialize() {
   alert("tst");
	AutoCompleteFT("1rPtLXKVn-fJ11xVbNoNrmlDgT8-JjhJ_0e5LkEY", "Utility", "acomplete");
}

      //google.setOnLoadCallback(AutoCompleteFT("1rPtLXKVn-fJ11xVbNoNrmlDgT8-JjhJ_0e5LkEY", "Utility", "acomplete"));
	  
    </script>
  </head>
  <body onload="initialize();">
    <div>
	<input type="text" id="acomplete" placeholder="Enter your address"></input>
    </div>
    <div id="visualization"></div>
  </body>
</html>